<template>
  <el-card>
    <el-tabs v-model="activeName">
      <el-tab-pane label="登录账户设置" name="loginInfo">
        <el-form
          ref="baseForm"
          :model="userInfo"
          :rules="rules"
          label-width="80px"
          style="width: 350px; margin: 50px auto"
        >
          <el-form-item label="姓名：" prop="username">
            <el-input v-model="userInfo.username"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input v-model="userInfo.password" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="saveProfile" type="primary">更新</el-button>
            <el-button @click="$router.back()">取消</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="个人详情" name="userInfo">
        <user-info />
      </el-tab-pane>
      <el-tab-pane label="岗位信息" name="jobInfo">
        <job-info />
      </el-tab-pane>
    </el-tabs>
  </el-card>
</template>

<script>
import { userProfile } from "@/api/user";
import { saveProfile } from "@/api/employees";

import userInfo from './components/user-info'
import jobInfo from './components/job-info'

export default {
  name: "userDetail",
  components:{
    userInfo,
    jobInfo
  },
  data() {
    return {
      activeName: "loginInfo",
      userInfo: {},
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },

  created() {
    // 获取用户基本资料
    this.getBaseProfile();
  },

  methods: {
    // 获取基本资料
    async getBaseProfile() {
      let res = await userProfile(this.$route.query.id);
      this.userInfo = res.data;
    },

    // 保存基本资料的更新事件
    saveProfile() {
      // 要做表单验证
      this.$refs.baseForm.validate(async (v) => {
        if (v) {
          // 可以发请求了
          await saveProfile(this.userInfo);
          this.$message.success("修改成功！");
        }
      });
    },
  },
};
</script>

<style>
</style>